package com.tdam2014.grupo02;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.tdam2014.grupo02.MainActivity;
import com.tdam2014.grupo02.R;
import com.tdam2014.grupo02.MainFragment.JsonReader;

import android.app.ListFragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FolderFragment extends ListFragment {

	private PictureSelectionListener pictureSelectionListener;
	private TextView txtFolderName;
	
	public static final String KEY_PICTURE_NAME = "keyName";
	public static final String KEY_LOAD_PICTURE_DATA = "keyLoadPersonData";
	private static boolean READ_ASYNC = true;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		return inflater.inflate(R.layout.fragment_folder, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		
		txtFolderName = (TextView) getView().findViewById(R.id.txtFolderName);
		if (getActivity().getIntent().getBooleanExtra(MainActivity.KEY_LOAD_FOLDER_DATA, false)) 
			
		{
			setFolderName(getActivity().getIntent().getStringExtra(MainActivity.KEY_FOLDER_NAME));
		}
			
		//ArrayList<Picture> list = Picture.generatePictures(getFolderName());
		//Picture.generatePictures(getFolderId(), getActivity());
		
		ArrayList<Picture> list = Picture.getPictures(getActivity(), getFolderId());
		if (READ_ASYNC) 
		{
			JsonReader reader = new JsonReader();
			reader.execute();
		}
		
		
		final PictureAdapter adapter = new PictureAdapter(getActivity());
		if (list != null)
		{
			adapter.setList(list);
		}
		else
		{
			adapter.setList(new ArrayList<Picture>());
		}
		
		setListAdapter(adapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
			{
				Picture picture = (Picture) adapter.getItem(position);
				if (pictureSelectionListener != null) 
				{
					pictureSelectionListener.onPictureSelected(picture);
				}
			}
		});
	}
	

	private int getFolderId() {
		// TODO Auto-generated method stub
		return Integer.parseInt(getActivity().getIntent().getStringExtra(MainActivity.KEY_FOLDER_ID));
	}

	public void setFolderName(String name) {
		txtFolderName.setText(name);
	}
	public String getFolderName ()
	{
		return (String) txtFolderName.getText();
	}
	public void setPictureSelectionListener(PictureSelectionListener listener) 
	{
		this.pictureSelectionListener = listener;
	}
	
	
	class JsonReader extends AsyncTask<Void, Void, Void> {
		
		public JsonReader() {}
			
		@Override
		protected Void doInBackground(Void... params) 
		{
			try {
				//Folder.updateFolder(getActivity());
				Picture.updateFolderPics(getActivity(), Folder.getFolder(getActivity(), String.valueOf(getFolderId())));
				
			} 
			catch (ClientProtocolException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
			return null;
		}

		@Override
		protected void onPostExecute(Void result) 
		{
			ArrayList<Picture> list = Picture.getPictures(getActivity(), getFolderId());
			final PictureAdapter adapter = new PictureAdapter(getActivity());
			if (list != null)
			{
				adapter.setList(list);
			}
			else
			{
				adapter.setList(new ArrayList<Picture>());
			}
			
			setListAdapter(adapter);
			
		}
	}
}
